package com.sun.lib.net.remote;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.orhanobut.logger.Logger;
import com.sun.lib.rx.Disposables;

import java.lang.reflect.ParameterizedType;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by sunxiquan on 2018/9/18.
 * <p>
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public abstract class DataObserver<T> implements Observer<Response<String>> {
    private Disposables disposables;

    public DataObserver(Disposables disposables) {
        this.disposables = disposables;
    }

    public DataObserver() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        if (disposables != null)
            disposables.add(d);
    }

    @Override
    public void onNext(Response<String> response) {
        /*Type genType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        Type type = params[0];*/
        if (response != null) {
            String json = response.body();
            if (!TextUtils.isEmpty(json)) {
                Logger.json(json);
            }
            Class<T> entiyClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            if (entiyClass == String.class) {
                onSuccess((T) json);
                return;
            }
            try {
                T t = new Gson().fromJson(json, entiyClass);
                onSuccess(t);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onError(Throwable e) {
        onFailed(e);
    }

    @Override
    public void onComplete() {
        onFinished();
    }

    protected abstract void onSuccess(T t);

    protected void onFailed(Throwable e) {

    }

    ;

    protected void onFinished() {
    }
}
